package com.test.qnews.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.activity.DetailsImgActivity;
import com.test.qnews.adapter.CommonAdapter;
import com.test.qnews.beans.GsonImgBean;
import com.test.qnews.beans.NetDataBean;
import com.test.qnews.beans.SingleImg;
import com.test.qnews.custom.RefreshableView;
import com.test.qnews.volley.VolleySingleton;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class BeautyFragmentSm extends Fragment{
    private CommonAdapter commonAdapter;
    private List<NetDataBean> data;
    private ListView listView;
    private Context context;

    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/1404457531633?client_os=android&client_version=2.1.0.1&bid=800&m_ch=008&city=0837&sn=97e463a2a58aa490289f6c346be4dec0&content_cnt=20&recoid=17773020978532888239&ftime=1452084681389&method=new&count=20";
    //刷新
    RefreshableView refreshableView;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.lv_recommend);
        refreshableView = (RefreshableView) view.findViewById(R.id.refreshable_view);

//        //调用刷新的方法
//        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
//            @Override
//            public void onRefresh() {
//                data();
//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                refreshableView.finishRefreshing();
//            }
//        }, 0);
//
        data();

    }

    private void data() {

        VolleySingleton.getInstance();
        VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                data = new ArrayList<>();
                GsonImgBean gsonBean = gson.fromJson(response, GsonImgBean.class);

                for (int i = 0; i < gsonBean.getData().getArticle().size(); i++) {
                    NetDataBean netDataBean = new NetDataBean();
                    netDataBean.setTitle(gsonBean.getData().getArticle().get(i).getTitle());
                    for (int j = 0; j < gsonBean.getData().getArticle().get(i).getImage().size(); j++) {
                        netDataBean.setImageUrl(gsonBean.getData().getArticle().get(i).getImage().get(0).getUrl());
                    }
                    netDataBean.setItem_type(gsonBean.getData().getArticle().get(i).getItem_type());
                    netDataBean.setImageSize(gsonBean.getData().getArticle().get(i).getImage().size());

                    for (int j = 0; j < gsonBean.getData().getArticle().get(i).getImage().size(); j++) {
//                        netDataBean.setImgUrls(gsonBean.getData().getArticle().get(i).getImage().get(j).getUrl());

                        netDataBean.setImgUrls(gsonBean.getData().getArticle().get(i).getImage().get(j).getUrl());
                    }

                    data.add(netDataBean);

                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(getActivity(), DetailsImgActivity.class);
                            intent.putExtra("pos", position);
                            intent.putExtra("data", (Serializable) data);
                            startActivity(intent);
                        }
                    });
                }

                commonAdapter = new CommonAdapter(data, context);
                listView.setAdapter(commonAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }
}

